import { ref, reactive } from 'vue'
import useUser from './useUser'

const useUserControl = ()=>{
    const { userInfo, getUserInfo, changeUserInfo, checkRepeatPass, changePassword } = useUser();
    
    const loading = ref(false);

    const errorModalState = reactive({
        visible: false, // 弹窗显示/隐藏
        errorText: '',  // 弹窗文案
    });

    const initData = () => {
        getUserInfo();
    }

    const onChangePassword = ({ oldPass, newPass }) => {
        // 判断两次密码是否一致
        if (checkRepeatPass(oldPass, newPass)) {
            changePassword();
        } else {
            errorModalState.visible = true;
            errorModalState.text = '两次输入的密码不一致，请修改'
        }
    }



    return {
        userInfo,
        // 初始化数据
        initData,
        // 修改密码
        onChangePassword,
        // 修改用户信息
        onChangeUserInfo: changeUserInfo,
    }
}

export default useUserControl